<?php
if (!class_exists('AlfaSmartyTemplateEngine')) {
	require_once("lib/smarty/Smarty.class.php");

	/**
	 * @author Alecsandru Gabriel DINU
	 * @since Jul 1, 2008
	 */	
	class AlfaSmartyTemplateEngine  {
		const TEMPLATES_PATH = "templates";
		const PAGES_PATH = "src/main/www/pages";
		private $smarty;
		private $pagesLocation;
		private $templatesLocation;
		private $templateName;

		public function __construct($templateName, $pageName){
			$config = AlfaConfiguration::getInstance();
			$this->pagesLocation = $config->getString("webapp/decorator/pagesLocation", self::PAGES_PATH);		
			$this->templatesLocation = $config->getString("webapp/decorator/templatesLocation", self::TEMPLATES_PATH);
		
			$this->templateName = $this->templatesLocation . "/" .$templateName;
			$this->smarty = new Smarty();

			$this->smarty->template_dir = $this->pagesLocation;
			$this->smarty->compile_dir  = $config->getString('cache/dir');
			$this->smarty->config_dir   = '';
			$this->smarty->cache_dir    = $config->getString('cache/dir');			
//			$this->smarty->caching = true;
			$this->smarty->left_delimiter = '%{';
			$this->smarty->right_delimiter = '}';			
			
			
			$this->smarty->assign("decoratedFile", "content/" . $pageName);			
		}

		public function addObject($objectName, $objectRef) {
//			$this->smarty->register_object($objectName,$objectRef);
			$this->smarty->assign($objectName,$objectRef);
		}
		
		public function mergeFunction($NameLst,$Value) {
			$this->smarty->register_function($NameLst, $Value);
		}

		public function mergeBlock($NameLst,$Value) {
			$this->smarty->register_block($NameLst, $Value);
		}
		
		function mergeField($NameLst,$Value=null) {
			$this->smarty->assign($NameLst,$Value);
		}
		
		public function show() {
			$this->smarty->display($this->templateName);
		}

	}
}